package com.wpml.cloud.myprovider.handler;

import com.wpml.cloud.myprovider.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class PersonHandler {

    @Autowired
    PersonService personService;

    public Mono<ServerResponse> getOne(ServerRequest request) {

        MultiValueMap<String, String> params = request.queryParams();

        String id = params.get("id").get(0);

        return ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(personService.getOne(Integer.valueOf(id))));
    }
}
